import 'package:flutter/widgets.dart';
import 'package:flutter_web_view/widget/f_webview/flutter_webview_pro_strategy.dart';
import 'package:flutter_web_view/util/platform_util.dart';
import 'package:flutter_web_view/widget/f_webview/webview_controller.dart';
import 'package:flutter_web_view/widget/f_webview/webview_strategy_type.dart';
import 'webview_windows_strategy.dart';
import 'in_app_webview_strategy.dart';

/// WebView 策略工厂
class FWebViewStrategyFactory {
  /// 根据平台，返回WebView实例
  static Widget getWebViewByPlatform(
    String initialUrl, {
    void Function(FWebViewController controller, String? url)? onTitleChanged,
    void Function(FWebViewController controller)? onWebViewCreated,
    void Function(FWebViewController controller, int progress)?
        onProgressChanged,
    void Function(FWebViewController controller, String? url)? onLoadStop,
  }) {
    WebViewStrategyType strategyType;
    if (PlatformUtil.isDesktop()) {
      strategyType = WebViewStrategyType.webviewWindows;
    } else {
      strategyType = WebViewStrategyType.webViewFlutterPro;
      // strategyType = WebViewStrategyType.inAppWebView;
    }
    return getWebViewByStrategy(strategyType, initialUrl,
        onTitleChanged: onTitleChanged,
        onWebViewCreated: onWebViewCreated,
        onLoadStop: onLoadStop,
        onProgressChanged: onProgressChanged);
  }

  /// 根据策略类型，返回WebView实例
  static Widget getWebViewByStrategy(
    WebViewStrategyType strategyType,
    String initialUrl, {
    void Function(FWebViewController controller, String? url)? onTitleChanged,
    void Function(FWebViewController controller)? onWebViewCreated,
    void Function(FWebViewController controller, int progress)?
        onProgressChanged,
    void Function(FWebViewController controller, String? url)? onLoadStop,
  }) {
    if (WebViewStrategyType.webviewWindows == strategyType) {
      return WebViewWindowsStrategy(
        initialUrl: initialUrl,
        onTitleChanged: onTitleChanged,
        onWebViewCreated: onWebViewCreated,
        onProgressChanged: onProgressChanged,
        onLoadStop: onLoadStop,
      );
    } else if (WebViewStrategyType.inAppWebView == strategyType) {
      return InAppWebViewStrategy(
        initialUrl: initialUrl,
        onTitleChanged: onTitleChanged,
        onWebViewCreated: onWebViewCreated,
        onProgressChanged: onProgressChanged,
        onLoadStop: onLoadStop,
      );
    } else {
      return FlutterWebViewProStrategy(
        initialUrl: initialUrl,
        onTitleChanged: onTitleChanged,
        onWebViewCreated: onWebViewCreated,
        onProgressChanged: onProgressChanged,
        onLoadStop: onLoadStop,
      );
    }
  }
}
